package com.mall.czlr.app.controller;

import com.mall.czlr.app.common.Const;
import com.mall.czlr.app.service.IUserService;
import com.mall.czlr.app.util.Util;
import com.mall.czlr.app.vo.ReqUserLoginVo;
import com.mall.czlr.app.vo.ReqUserRegisterVo;
import com.mall.czlr.base.annotation.Login;
import com.mall.czlr.base.common.DataResponse;
import com.mall.czlr.base.controller.BaseController;
import com.mall.czlr.base.exception.BaseException;
import com.mall.czlr.base.util.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = {"用户管理"})
public class UserController extends BaseController {

    private final IUserService userService;
    private final HttpServletRequest request;

    @Autowired
    public UserController(HttpServletRequest httpServletRequest, IUserService userService, HttpServletRequest request) {
        super(httpServletRequest);
        this.userService = userService;
        this.request = request;
    }

    @ApiOperation("注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @Login(needToken = false)
    public DataResponse register(@Valid ReqUserRegisterVo req) throws Exception {
        String username = req.getUsername().trim();
        String password = req.getPassword().trim();
        String code = req.getCode().trim();

        if (!code.equals("9999")) {
            Assert.exception(BaseException.ERROR_VALIDATE_CODE, "验证码错误");
        }
        Map<String, Object> data = userService.register(username, password);
        return DataResponse.create().putAll(data);
    }

    @ApiOperation("登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @Login(needToken = false)
    public DataResponse login(@Valid ReqUserLoginVo req) throws Exception {
        String username = req.getUsername().trim();
        String password = req.getPassword().trim();

        Map<String, Object> data = userService.login(username, password);
        return DataResponse.create().putAll(data);
    }

    @ApiOperation("登出")
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    public DataResponse logout() {
        Util.checkAuth(getUserContext(), Const.USER_TYPE_USER);
        String token = request.getAttribute("assess_token").toString();

        userService.logout(token);
        return DataResponse.create();
    }

    @ApiOperation("详情")
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public DataResponse userInfo() {
        Util.checkAuth(getUserContext(), Const.USER_TYPE_USER);
        Long userId = getUserContext().getUserId();

        Map<String, Object> data = userService.getUserInfo(userId);
        return DataResponse.create().putAll(data);
    }

    @ApiOperation("更新信息")
    @RequestMapping(value = "/info-update", method = RequestMethod.POST)
    public DataResponse updateUserInfo(@RequestParam(value = "nickname", required = false, defaultValue = "") String nickname,
                                        @RequestParam(value = "avatar", required = false, defaultValue = "") String avatar) {
        Util.checkAuth(getUserContext(), Const.USER_TYPE_USER);
        Long userId = getUserContext().getUserId();

        userService.updateUserInfo(userId, nickname, avatar);
        return DataResponse.create();
    }
}
